package cn.vihub.controller;

import cn.vihub.param.ShopGoodsCategoryParam;
import cn.vihub.param.ShopGoodsSpecParam;
import cn.vihub.pojo.ShopGoodsCategory;
import cn.vihub.pojo.ShopGoodsSpec;
import cn.vihub.service.ShopGoodsSpecService;
import cn.vihub.utils.Result;
import cn.vihub.vo.ShopGoodsSpecVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 秋晚
 * @Date: 2025/7/28 20:10
 * @Description:
 **/
@RestController
@RequestMapping("shop-store/shopGoodsSpec")
public class ShopGoodsSpecController {

    @Autowired
    ShopGoodsSpecService shopGoodsSpecService;

    /**
     * @author: 秋晚
     * @description:查询属性数据
     * @params: [cn.vihub.param.ShopGoodsSpecParam]
     * @return: cn.vihub.utils.Result<com.baomidou.mybatisplus.core.metadata.IPage<cn.vihub.vo.ShopGoodsSpecVo>>
     * @date: 2025/7/28 20:54
     */
    @GetMapping("getShopGoodsSpec")
    public Result<IPage<ShopGoodsSpecVo>> getShopGoodsSpec(ShopGoodsSpecParam shopGoodsSpecParam){
        //查询
        return Result.success(shopGoodsSpecService.getShopGoodsSpec(shopGoodsSpecParam));
    }

  @GetMapping("goodsSpectNotPage")
    public Result<List<ShopGoodsSpec>> goodsSpectNotPage(){
        //查询
        return Result.success(shopGoodsSpecService.list());
    }


    /**
     * @author: 秋晚
     * @description:添加或编辑分类
     * @params: [cn.vihub.param.ShopGoodsCategoryParam]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/28 20:09
     */
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody ShopGoodsSpecParam shopGoodsSpecParam){
        //创建对象
        ShopGoodsSpec shopGoodsSpec = new ShopGoodsSpec();
        //拷贝
        BeanUtils.copyProperties(shopGoodsSpecParam,shopGoodsSpec);
        //将路径处理获取最后一个id值赋值给分类id
        String[] split = shopGoodsSpec.getGoodsCategoryPath().split(",");
        Integer categoryId = Integer.valueOf(split[split.length - 1]);
        shopGoodsSpec.setGoodsCategoryId(categoryId);
        //返回
        return Result.success(shopGoodsSpecService.saveOrUpdate(shopGoodsSpec));
    }


    /**
     * @author: 秋晚
     * @description:根据id删除分类
     * @params: [java.lang.Integer]
     * @return: cn.vihub.utils.Result<java.lang.Boolean>
     * @date: 2025/7/28 20:10
     */
    @DeleteMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") Integer id){
        return Result.success(shopGoodsSpecService.removeById(id));
    }


}
